<?php

namespace backend\modules\goods\models;

use backend\modules\comLib\ImgLib;
use Yii;
use yii\base\Exception;

/**
 * This is the model class for table "goods".
 *
 * @property string $goods_id
 * @property string $goods_name
 * @property string $shop_price
 * @property string $market_price
 * @property string $goods_image
 * @property string $gall_images_id
 * @property string $desc_images_id
 * @property integer $is_new
 * @property integer $is_hot
 * @property integer $is_good
 * @property integer $is_promote
 * @property integer $is_delete
 * @property string $cate_id
 * @property string $goods_desc
 * @property string $is_single
 * @property string $attr_cat_id
 * @property string $is_gift
 * @property string $attrText
 * @property string $weight
 * @property string $thumb_img
 */
class Goods extends \yii\db\ActiveRecord
{
    public $gallImages;

    public $descImages;
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'goods';
    }

    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
//            TimestampBehavior::className(),
            // 名为myBehavior4的行为，也是给出了MyBehavior类的配置数组
            'uuid' => [
                'class' => \common\behavior\uuidBehavior::className(),
                'owner' => $this
            ]
        ];
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            ['goods_id','default','value'=>$this->getUUID()],
            [['goods_id','goods_name','market_price','shop_price'], 'required'],
            [['shop_price', 'market_price','weight'], 'number'],
            ['is_new','default','value'=>1],
            ['is_hot','default','value'=>1],
            ['is_good','default','value'=>1],
            ['is_promote','default','value'=>0],
            ['is_delete','default','value'=>0],
            ['is_single','default','value'=>1],
            [['is_new', 'is_hot', 'is_gift','is_good', 'is_promote', 'is_delete'], 'integer'],
            [['goods_id', 'gall_images_id', 'desc_images_id', 'cate_id','attr_cat_id'], 'string', 'max' => 40],
            [['goods_name'], 'string', 'max' => 60],
            [['goods_image','thumb_img'], 'string', 'max' => 255],
            [['goods_name'], 'unique'],
            [['goods_desc','attrText'], 'string'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'goods_id' => Yii::t('app', 'Goods ID'),
            'goods_name' => Yii::t('app', '商品名称'),
            'shop_price' => Yii::t('app', '本店价格'),
            'market_price' => Yii::t('app', '市场原价'),
            'goods_image' => Yii::t('app', '商品图片'),
            'gall_images_id' => Yii::t('app', 'Gall Images ID'),
            'desc_images_id' => Yii::t('app', 'Desc Images ID'),
            'is_new' => Yii::t('app', '是否新品'),
            'is_hot' => Yii::t('app', '是否热销品'),
            'is_good' => Yii::t('app', '是否精品'),
            'is_promote' => Yii::t('app', '是否促销品'),
            'is_delete' => Yii::t('app', '是否已删除 '),
            'cate_id' => Yii::t('app', '商品分类'),
            'goods_desc'=>Yii::t('app', '商品描述'),
            'is_single'=>Yii::t('app', '是否单品'),
            'attr_cat_id'=>Yii::t('app', '属性分类'),
            'is_gift'=>Yii::t('app', '是否可分享返利'),
            'attrText'=>Yii::t('app', '属性text'),
            'weight'=>Yii::t('app', '重量'),
        ];
    }

    /**
     * @param $id
     * @return mixed|string
     */
    public function findImage($id)
    {
        $GoodsGallImages  = new GoodsGallImages();
        $goods_gall_image = '';
        $GoodsGallImages = $GoodsGallImages->find()->select(['goods_gall_image'])->where(['goods_id'=>$id])->orderBy(['sort_number'=>SORT_DESC])->limit(1)->one();
        if($GoodsGallImages) {
            $goods_gall_image = $GoodsGallImages->goods_gall_image;
        }
        return $goods_gall_image;
    }

    public static function  isGoodAlias($type)
    {
        if(empty($type)){
            return "非精品";
        }elseif($type=="all") {
            return ["非精品","精品"];
        } else if($type==1){
            return "精品";
        }
    }

    public static function  isHotAlias($type)
    {
        if(empty($type)){
            return "非热销品";
        }elseif($type=="all") {
            return ["非热销品","热销品"];
        } else if($type==1){
            return "热销品";
        }
    }

    public static function  isNewAlias($type)
    {
        if(empty($type)){
            return "非新品";
        }elseif($type=="all") {
            return ["非新品","新品"];
        } else if($type==1){
            return "新品";
        }
    }

    public static function  isDeleteAlias($type)
    {
        if(empty($type)){
            return "非下架";
        }elseif($type=="all") {
            return ["非下架","下架"];
        } else if($type==1){
            return "下架";
        }
    }

    public static function  isPromoteAlias($type)
    {
        if(empty($type)){
            return "非促销";
        }elseif($type=="all") {
            return ["非促销","促销"];
        } else if($type==1){
            return "促销";
        }
    }

    public static function isSingleAlias($type)
    {
        if(empty($type)){
            return "非单品";
        }elseif($type=="all") {
            return ["非单品","单品"];
        } else if($type==1){
            return "单品";
        }
    }

    /**
     * 处理商品图片排序和商品ID图
     *
     * @param $goods_id
     * @throws Exception
     */
    public static function SortingImages($goods_id){
        $goods = Goods::findOne($goods_id);

        $modelSort = GoodsGallImages::find()->where(['goods_id'=>$goods_id])->orderBy(['sort_number'=>SORT_ASC])->all();
        $goods->goods_image = $modelSort[0]->goods_gall_image;

        static $i  = 0;
        foreach($modelSort as $k=>$v) {
            $v->sort_number = $i++;
            if(!$v->save()){
                throw new Exception(json_encode($v->errors));
            }
        }
        if(!$goods->save()){
            throw new Exception(json_encode($goods->errors));
        }
    }

    /**
     * 获取属性分类
     *
     * @param $attr_cat_id
     * @return array
     */
    public static function getArbByArc($attr_cat_id)
    {
        $attribute = Attribute::find()->where(['attr_cat_id'=>$attr_cat_id])->asArray()->all();
        $attrvalueARR = array();
        foreach($attribute as $k=>$v) {
            $attr_id = $v['attr_id'];
            $Attrvalue = Attrvalue::find()->where(['attr_id'=>$attr_id])->asArray()->all();
            $v['children'] = $Attrvalue;
            $attrvalueARR[] = $v;
        }
        return $attrvalueARR;
    }

    /**
     * 获取属性分类缺口
     *
     * @param $attr_cat_id
     * @return array
     */
    public static function getArbByArcandgoods($attr_cat_id,$goods_id)
    {
        $attribute = Attribute::find()->where(['attr_cat_id'=>$attr_cat_id])->asArray()->all();
        $attrvalueARR = array();
        foreach($attribute as $k=>$v) {
            $attr_id = $v['attr_id'];
            $Attrvalue = Attrvalue::find()->where(['attr_id'=>$attr_id])->asArray()->all();
            foreach($Attrvalue as $key=>$value){
                $AttrvalueGoods = AttrvalueGoods::find()->where(['attr_value_id'=>$value['attr_value_id'],'goods_id'=>$goods_id])->one();
                if($AttrvalueGoods){
                    $Attrvalue[$key]['check'] = 1;
                }
            }
            $v['children'] = $Attrvalue;
            $attrvalueARR[] = $v;
        }
        return $attrvalueARR;
    }

    /**
     * 获取属性
     *
     * @param $attr_cat_id
     * @return array
     */
    public static function getAttributeByArc($attr_cat_id)
    {
        $attribute = Attribute::find()->where(['attr_cat_id'=>$attr_cat_id])->asArray()->all();print_r($attribute);exit;
        return $attribute;
    }


    /**
     * 同一属性分类 判断是否重复属性
     *
     * @param $goods_id
     * @param $attr_cat_id
     * @param $attrARR
     * @return bool
     */
    public static function isReloadAttr($goods_id,$attr_cat_id,$attrARR)
    {
        $compareARR = static::findAttrgoods($attr_cat_id,$goods_id);

        foreach($compareARR as $k=>$v) {
            if($v==$attrARR) {
                return true;
            }
        }
        return false;
    }

    /**
     * 根据属性分类查找所有商品选中的属性值
     * key=>attr_id  value=>attr_value_id
     *
     * @param $attr_cat_id
     * @param $goods_id
     * @return array
     */
    public static function findAttrgoods($attr_cat_id,$goods_id)
    {
        $goods_idsARR = Goods::find()->where(['is_delete'=>0,'is_single'=>0,'attr_cat_id'=>$attr_cat_id])->select(['goods_id'])->asArray()->all();
        $compareARR = array();
        foreach($goods_idsARR as $k=>$v){
            $AttrvalueGoods = AttrvalueGoods::find()->where(['goods_id'=>$v['goods_id']])->asArray()->all();
            if($AttrvalueGoods){
                foreach($AttrvalueGoods as $key=>$val){
                    if($val){
                        $compareARR[$k][$val['attr_id']] = $val['attr_value_id'];
                    }
                }
            }
        }
        return $compareARR;
    }

//    public static function
}
